/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.aice.chinamapview.component;

import com.aice.chinamapview.util.Log;
import com.aice.chinamapview.ResourceTable;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;
import ohos.multimodalinput.event.TouchEvent;

import java.io.IOException;

/**
 * 自定义button
 *
 * @author jjy
 * @since 2021-05-10
 */
public class HmButton extends Button implements Component.TouchEventListener {
    private static final String TAG = "HmButton";
    private Context mContext;
    private int clickColor;
    private int buttonColor;

    /**
     *
     * 构造函数
     *
     * @param context context对象
     */
    public HmButton(Context context) {
        super(context);
    }

    /**
     *
     * 构造函数
     *
     * @param context context对象
     * @param attrSet 属性值
     */
    public HmButton(Context context, AttrSet attrSet) {
        super(context, attrSet);
        mContext = context;
        initData();
        this.setTouchEventListener(this::onTouchEvent);
    }

    /**
     *
     * 构造函数
     *
     * @param context context对象
     * @param attrSet 属性值
     * @param styleName 属性名
     */
    public HmButton(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    private void initData() {
        ResourceManager resManager = mContext.getResourceManager();
        try {
            clickColor = resManager.getElement(ResourceTable.Color_buttonClickColor).getColor();
            buttonColor = resManager.getElement(ResourceTable.Color_buttonColor).getColor();
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
        } catch (NotExistException e) {
            Log.e(TAG, e.getMessage());
        } catch (WrongTypeException e) {
            Log.e(TAG, e.getMessage());
        }
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        switch (touchEvent.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                ShapeElement element = new ShapeElement();
                element.setRgbColor(RgbColor.fromArgbInt(clickColor));
                this.setBackground(element);
                return true;
            case TouchEvent.POINT_MOVE:
                return true;
            case TouchEvent.PRIMARY_POINT_UP:
                ShapeElement backelement = new ShapeElement();
                backelement.setRgbColor(RgbColor.fromArgbInt(buttonColor));
                this.setBackground(backelement);
                return true;
            default:
        }
        return false;
    }
}
